package com.lam.mall.controller;

import java.util.Date;
import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import com.lam.common.validate.ValidateUtil;
import com.lam.common.validate.model.ValidOperType;
import com.lam.mall.common.constant.MallConstant;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lam.framework.operLog.annotation.Log;
import com.lam.common.components.dict.annotation.DictAutoTransfer;
import com.lam.common.context.TokenUtils;
import com.lam.common.utils.NumberUtil;
import com.lam.framework.annotation.ParamInject;
import com.lam.common.base.controller.BaseController;
import com.lam.common.base.domain.Result;
import com.lam.framework.operLog.enums.BusinessType;
import com.lam.mall.entity.Notice;
import com.lam.mall.param.NoticeParam;
import com.lam.mall.service.NoticeService;
import org.springframework.validation.annotation.Validated;
import com.lam.common.validate.group.Update;
import com.lam.common.validate.group.Add;

/**
 * 公告管理Controller
 * 
 * @author admin
 * @date 2023-04-20
 */
@Log(title = "公告管理")
@ParamInject
@RestController
@RequestMapping("/${adminPath}/mall/notice")
public class NoticeController extends BaseController {

    @Autowired
    private NoticeService noticeService;

    /**
     * 查询公告管理列表
     */
    @Log(oper="分页查询")
    @DictAutoTransfer
    @PreAuthorize("@ss.hasPm('mall:notice:list')")
    @GetMapping("/list")
    public Result list(NoticeParam param) {
    
    	param.setShopId(TokenUtils.getLoginUserShopId());
        startPage();
        List<Notice> list = noticeService.selectList(param);
        return Result.ok(getTableData(list));
    }

    /**
     * 获取公告管理详细信息
     */
    @DictAutoTransfer
    @Log(oper="根据ID查询")
    @PreAuthorize("@ss.hasPm('mall:notice:query')")
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable("id") String id) {
    
        return Result.ok(noticeService.selectById(id));
    }

    /**
     * 新增公告管理
     */
    @PreAuthorize("@ss.hasPm('mall:notice:add')")
    @Log(businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@Validated(Add.class) @RequestBody Notice notice) {
    
    	notice.setShopId(TokenUtils.getLoginUserShopId());
        ValidateUtil.validUnique(notice, ValidOperType.ADD, MallConstant.UNIQUE_NOTICE_TITLE, "公告标题已存在！");
        if(NumberUtil.eq(notice.getStatus(), 1)) {
        	notice.setPublishTime(new Date());
        }
        int n = noticeService.insert(notice);
        if(n>0) {
        	noticeService.removeNoticeByIdCache(notice.getId());
        	noticeService.removeNoticeCache();
        }
        return toAjax(n);
    }

    /**
     * 修改公告管理
     */
    @PreAuthorize("@ss.hasPm('mall:notice:edit')")
    @Log(businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@Validated(Update.class) @RequestBody Notice notice) {
    
    	notice.setShopId(TokenUtils.getLoginUserShopId());
        ValidateUtil.validUnique(notice, ValidOperType.UPDATE, MallConstant.UNIQUE_NOTICE_TITLE, "公告标题已存在！");
        Notice dbNotice = noticeService.selectById(notice.getId());
        if(NumberUtil.eq(dbNotice.getStatus(), 0) && NumberUtil.eq(notice.getStatus(), 1)) {
        	notice.setPublishTime(new Date());
        }
        int n = noticeService.update(notice);
        if(n>0) {
            noticeService.removeNoticeByIdCache(notice.getId());
        	noticeService.removeNoticeCache();
        }
        return toAjax(n);
    }

    /**
     * 删除公告管理
     */
    @PreAuthorize("@ss.hasPm('mall:notice:remove')")
    @Log(businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public Result remove(@PathVariable String id) {
    
        int n = noticeService.deleteById(id);
        if(n>0) {
            noticeService.removeNoticeByIdCache(id);
        	noticeService.removeNoticeCache();
        }
        return toAjax(n);
    }

}
